﻿using Common;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace CarPCBATest.Procedure
{
    internal class PcbConfigHelper : NotifyBase
    {
        private static string dirBackup = "config_backup";
        private static string fileName = "TestConfig.xml";
        public static AllPcbConfig AllPcbConfig { get; set; }

        public static AllPcbConfig CreateSample()
        {
            PcbConfig pcbConfig1 = new PcbConfig()
            {
                Name = "A控制板L",
                VisionProjPath = "VisionProjPath",
                BurnConfig = new BurnConfig
                {
                    Paud = 500000,
                    Port = "COM1",

                    SubBurnConfigs = new ObservableCollection<SubBurnConfig>
                       {
                           new SubBurnConfig{
                            BrunFilePath = "BurnPath1",

                               McuAddress =1
                           },
                           new SubBurnConfig{
                             BrunFilePath = "BurnPath2",

                               McuAddress =2
                           }
                       },
                },

                ListLedCurrentConfig = new ObservableCollection<LedCurrentConfig>
                {
                    new LedCurrentConfig()
                    {
                        LedDesc = "SL",
                        LowerLimitCurrent = 408,
                        UpperLimitCurrent = 1907,
                         ProcName = "SL Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig()
                    {
                        LedDesc = "TL",
                        LowerLimitCurrent = 70,
                        UpperLimitCurrent = 282,
                        ProcName = "TL Vision Test",
                         WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TL+GATE",
                        LowerLimitCurrent = 169,
                        UpperLimitCurrent = 709,
                        ProcName = "TL + GATE Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig
                    {
                        LedDesc = "RTSL",
                        LowerLimitCurrent = 41,
                        UpperLimitCurrent = 153,
                         ProcName = "RTSL Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig
                    {
                        LedDesc = "RTSL+GATE",
                        LowerLimitCurrent = 119,
                        UpperLimitCurrent = 430,
                         ProcName = "RTSL+GATE Vision Test",
                         WaterFailLight = true
                    }
                }
                ,
                No = 0,
                ListPowerConfig = new ObservableCollection<PowerConfig>()
                 {
                     new PowerConfig ()
                     {
                         ChannelIndex = 0,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 1,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 2,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     }
                 }
            };
            PcbConfig pcbConfig2 = new PcbConfig()
            {
                Name = "B控制板L",
                VisionProjPath = "VisionProjPath",
                BurnConfig = new BurnConfig
                {
                    Paud = 500000,
                    Port = "COM1",

                    SubBurnConfigs = new ObservableCollection<SubBurnConfig>
                       {
                           new SubBurnConfig{
                            BrunFilePath = "BurnPath1",

                               McuAddress =1
                           },
                           new SubBurnConfig{
                             BrunFilePath = "BurnPath2",

                               McuAddress =2
                           }
                       },
                },

                ListLedCurrentConfig = new ObservableCollection<LedCurrentConfig>
                {
                    new LedCurrentConfig()
                    {
                        LedDesc = "SL",
                        LowerLimitCurrent = 408,
                        UpperLimitCurrent = 1907,
                         ProcName = "SL Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig()
                    {
                        LedDesc = "TL",
                        LowerLimitCurrent = 70,
                        UpperLimitCurrent = 282,
                        ProcName = "TL Vision Test",
                         WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TL+GATE",
                        LowerLimitCurrent = 169,
                        UpperLimitCurrent = 709,
                        ProcName = "TL + GATE Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig
                    {
                        LedDesc = "RTSL",
                        LowerLimitCurrent = 41,
                        UpperLimitCurrent = 153,
                         ProcName = "RTSL Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig
                    {
                        LedDesc = "RTSL+GATE",
                        LowerLimitCurrent = 119,
                        UpperLimitCurrent = 430,
                         ProcName = "RTSL+GATE Vision Test",
                         WaterFailLight = true
                    }
                }
                ,
                No = 1,
                ListPowerConfig = new ObservableCollection<PowerConfig>()
                 {
                     new PowerConfig ()
                     {
                         ChannelIndex = 0,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 1,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 2,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     }
                 }
            };
            PcbConfig pcbConfig3 = new PcbConfig()
            {
                Name = "C控制板L",
                VisionProjPath = "VisionProjPath",
                BurnConfig = new BurnConfig
                {
                    Paud = 500000,
                    Port = "COM4",

                    SubBurnConfigs = new ObservableCollection<SubBurnConfig>
                       {
                           new SubBurnConfig{
                            BrunFilePath = "BurnPath1",

                               McuAddress =1
                           },
                           new SubBurnConfig{
                             BrunFilePath = "BurnPath2",

                               McuAddress =2
                           }
                       },
                },

                ListLedCurrentConfig = new ObservableCollection<LedCurrentConfig>
                {
                    new LedCurrentConfig()
                    {
                        LedDesc = "SL",
                        LowerLimitCurrent = 408,
                        UpperLimitCurrent = 1907,
                         ProcName = "SL Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig()
                    {
                        LedDesc = "TL",
                        LowerLimitCurrent = 70,
                        UpperLimitCurrent = 282,
                        ProcName = "TL Vision Test",
                         WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TL+GATE",
                        LowerLimitCurrent = 169,
                        UpperLimitCurrent = 709,
                        ProcName = "TL + GATE Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig
                    {
                        LedDesc = "RTSL",
                        LowerLimitCurrent = 41,
                        UpperLimitCurrent = 153,
                         ProcName = "RTSL Vision Test",
                         WaterFailLight = true
                    },
                    new LedCurrentConfig
                    {
                        LedDesc = "RTSL+GATE",
                        LowerLimitCurrent = 119,
                        UpperLimitCurrent = 430,
                         ProcName = "RTSL+GATE Vision Test",
                         WaterFailLight = true
                    }
                }
                ,
                No = 2,
                ListPowerConfig = new ObservableCollection<PowerConfig>()
                 {
                     new PowerConfig ()
                     {
                         ChannelIndex = 0,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 1,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 2,
                          IsActive = true,
                           Current = 8,
                            Voltage= 13.5,
                     }
                 }
            };

            //if (File.Exists(fileName))
            //{
            //    return null;
            //}
            AllPcbConfig config = new AllPcbConfig();
            config.DM3025EPort = "COM1";
            config.IT6302APort = "COM2";
            config.Title = "TITLE";
            config.ListConfig.Add(pcbConfig1);
            config.ListConfig.Add(pcbConfig2);
            config.ListConfig.Add(pcbConfig3);
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
            return config;
        }

        public static PcbConfig GetTestConfig()
        {
            var config = Load();
            return config.ListConfig.Where(t => t.No == config.UsingNo).ToList()[0];
        }

        public static AllPcbConfig Load()
        {
            if (!File.Exists(fileName))
            {
                return CreateSample();
            }
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));

            TextReader reader = new StreamReader(fileName);
            // 初始化默认值

            var obj = serializer.Deserialize(reader);
            reader.Close();
            AllPcbConfig = obj as AllPcbConfig;
            return obj as AllPcbConfig;
        }

        public static void Save(AllPcbConfig config)
        {
            AllPcbConfig = config;
            if (!Directory.Exists(dirBackup))
            {
                Directory.CreateDirectory(dirBackup);
            }
            string tmp = $"./{dirBackup}/config-{DateTime.Now.ToString("yyyyMMdd")}.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(tmp);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            File.Copy(tmp, fileName);
        }

        public static void Save()
        {
            var config = AllPcbConfig;
            if (!Directory.Exists(dirBackup))
            {
                Directory.CreateDirectory(dirBackup);
            }
            string tmp = $"./{dirBackup}/config-{DateTime.Now.ToString("yyyyMMdd")}.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(tmp);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            File.Copy(tmp, fileName);
        }
    }
}